package com.example.unisys.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.unisys.common.Response;
import com.example.unisys.common.STATUS;
import com.example.unisys.entity.School;
import com.example.unisys.entity.User;
import com.example.unisys.service.SchoolService;
import com.example.unisys.service.UserService;
import com.example.unisys.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping(value = "/apis/v1/school/", produces = "application/json; charset=utf-8")
public class SchoolController {

    @Autowired
    SchoolService schoolService;

    @Autowired
    UserService userService;

    /*
    * 获取学校信息
    * */
    @RequestMapping(value = "", method = {RequestMethod.GET})
    public Response getNotice(@CookieValue("Admin-Token") String token) {
        HashMap<String, Object> res = new HashMap();
        String username = JWTUtil.getUsername(token);
        User user = userService.getInfo(username);
        String main_school = user.getMain_school();
        String sub_school = user.getSub_school();
        School school = schoolService.getSchool(main_school, sub_school);
        return Response.builder().status(STATUS.SUCCESS).data(school).build();
    }

    /*
    * 修改学校信息
    * */
    @RequestMapping(value = "", method = {RequestMethod.PUT})
    public Response modifySchool(@RequestBody JSONObject jsonParam) {
        School school = JSONObject.parseObject(jsonParam.toJSONString(), School.class);
        int ret_code = schoolService.update(school);
        if (ret_code == 1)
            return Response.builder().status(STATUS.SUCCESS).build();
        return Response.builder().status(STATUS.BAD_REQUEST).build();
    }


}
